$(document).ready(function() {
	menuEffect();
	preventSelection();

	fullPageEffect();

	$(window).scroll(function() {

	});

	$(window).resize(function() {
		fullPageEffect();
		if(viewport().width > 768) {
			
		}
		// resize_product_thumb();
	});

	// homePage();
});

function viewport() {
    var e = window, a = 'inner';
    if (!('innerWidth' in window )) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

// function resize_product_thumb() {
// 	w = $('.product-list li .p-thumb').innerWidth();
// 	b_h = w * 1.43;
// 	s_h = w * 1.24;
// 	$('.product-list.big-show li .p-thumb').css('height', b_h+'px');
// 	$('.product-list.small-show li .p-thumb').css('height', s_h+'px');
	
// 	$('.product-list li .p-thumb img').css('width','0');
// 	setTimeout(function() {
// 		$('.product-list li .p-thumb img').css('width','auto');
// 	}, 20);
// }

function menuEffect() {
	// Hide/Show Menu Panel
	$('#menu-button').click(function() {
		$('.main-menu').toggleClass('show');
		// $('.main-page').toggleClass('hide-left');
		$('#topbar').toggleClass('hide-left');

		// $('#main-timeline').toggle(); // For History Main Page Timeline
	});

	// Hide/Show User Actions Group
	$('#user-button').click(function(e) {
		$('#topbar .user-group').slideToggle('fast');
	});
	$(document).mouseup(function (e) {
		var container = $('#topbar .user-group');
		var button = $('#user-button');

	    if( !container.is(e.target) // if the target of the click isn't the container...
	        && container.has(e.target).length === 0 // ... nor a descendant of the Container
	        && !button.is(e.target) // if the target of the click isn't the User Button...
	        && button.has(e.target).length === 0) // ... nor a descendant of the User Button
	    {
	    	container.slideUp('fast');
	    }

	    var sidebar = $('.main-menu .category-menu');
	    var topbar = $('#topbar');
	    if( !sidebar.is(e.target) // if the target of the click isn't the container...
	        && sidebar.has(e.target).length === 0 // ... nor a descendant of the Container
	        && !topbar.is(e.target) // if the target of the click isn't the User Button...
	        && topbar.has(e.target).length === 0) // ... nor a descendant of the User Button

	    {
	    	$('.main-menu').removeClass('show');
	    	$('.main-page').removeClass('hide-left');
	    	$('#topbar').removeClass('hide-left');
			// $('#main-timeline').hide(); // For History Main Page Timeline
	    }
	});
}

function fullPageEffect() {
	fullPageSectionHeight = viewport().height - 35;
	$('.full-page').css('min-height', fullPageSectionHeight+'px');

	$('.full-page').each(function() {
		var element = this;
		if ($(this).css('background-image') != 'none') {
			elWidth = $('.full-page').width();
			elHeight = $('.full-page').height();

			var img = new Image();
			img.src = $(this).css('background-image').replace(/url\(|\)$/ig, "");
			console.log(img.src);

			// img.onload = function() {
			// 	bgImgWidth = this.width;
			// 	bgImgHeight = this.height;
				
			// 	if((bgImgWidth/elWidth) > (bgImgHeight/elHeight)) {
			// 		bgW = bgImgWidth / bgImgHeight * elHeight;
			// 		$(element).css('background-size', bgW + 'px ' + elHeight + 'px');
			// 		console.log(bgW);
			// 	}
			// 	else {
			// 		bgH = bgImgHeight / bgImgWidth * elWidth;
			// 		$(element).css('background-size', elWidth + 'px ' + bgH + 'px');
			// 	}
			// }				
		}			
	});
		
	// $('.full-page').css('background-size', fW + 'px ' + fH + 'px');
}

function preventSelection() {
	$('#topbar').mousedown(function(e) { 
		e.preventDefault(); 
	});
}

function homePage() {
	// Cover Img
	$('.thumb').each(function() {
		console.log($(this));
	});
}
